package com.cloudcc.frame.trigger;

import com.cloudcc.deveditor.core.CCObject;
import com.cloudcc.deveditor.core.CCService;
import com.cloudcc.deveditor.core.ServiceResult;
import com.cloudcc.deveditor.trigger.TriggerEditor;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class Manage_Group  extends TriggerEditor {

    /*
     * @Description: 判断是否存在管护权
     * @T(n):
     * @Author: zxb
     * @Date:
     * @param null:
     * @return: null
     **/
    public void beforeinsert(){
        String ghzt_continue="已失效";
        //公共资源交易中心
        String fk_psmc=record_new.get("fk_psmc")==null?"":record_new.get("fk_psmc")+"";
        //客户
        String fk_account=record_new.get("fk_account")==null?"":record_new.get("fk_account")+"";
        String check_sql="select id from manage_group " +
                "where fk_psmc='"+fk_psmc+"' " +
                "and is_deleted='0' " +
                "and ghzt!='"+ghzt_continue+"' " +
                "and fk_account='"+fk_account+"'";
        List<CCObject> manage_group_list = this.cqlQuery("manage_group", check_sql);
        if(manage_group_list.size()>0){
            if(true)trigger.addErrorMessage("当前交易中心已存在管户经理");
        }

    }
    /*
     * @Description:
     * 1 获取变量与定义常量
     * 2判断是否需要写入共享规则：
     * 是的话，写入共享规则；
     * @T(n):
     * @Author: zxb
     * @Date:
     * @param null:
     * @return: null
     **/
    public void afterinsert(){
        String YES="是";
        //管户经理
        String fk_ccuser=record_new.get("fk_ccuser")==null?"":record_new.get("fk_ccuser")+"";
        //id
        String id=record_new.get("id")==null?"":record_new.get("id")+"";
        //是否写入共享
        String is_shared=record_new.get("is_shared")==null?"":record_new.get("is_shared")+"";
        //客户id
        String fk_account=record_new.get("fk_account")==null?"":record_new.get("fk_account")+"";
        //统一社会信用代码
        String name=record_new.get("name")==null?"":record_new.get("name")+"";

        if("".equals(name)){
            String code_sql="select invoiceTaxNo from Account where id='"+fk_account+"' and is_deleted='0'";
            List<CCObject> account = this.cqlQuery("Account", code_sql);
            if(account.size()>0){
                String invoiceTaxNo = account.get(0).get("invoiceTaxNo") == null ? "" : account.get(0).get("invoiceTaxNo") + "";
                this.cqlQuery("Account","update manage_group set name='"+invoiceTaxNo+"' where id='"+id+"'");
            }
        }

        if(is_shared.equals(YES)){
            //创建共享对象
            CCObject Account_share = new CCObject("Account",CCObject.IS_SHARED);
            Account_share.put("parentid",fk_account);
            Account_share.put("accesslevel", "Write");
            Account_share.put("rowcause", "Manual");
            Account_share.put("userorgroupid",fk_ccuser);
            this.insert(Account_share);
        }

    }
    /*
     * @Description:
     * 1获取所有需要的变量和常量的初始化
     * 2判断管户状态是否失效
     * 3判断管户小组是否执行了共享规则：
     * a如果执行了，那么去删掉之前的共享表
     * b如果未执行，变更当前客户的所有人为管户经理的公海池绑定用户
     *
     * @T(n):
     * @Author: zxb
     * @Date:
     * @param null:
     * @return: null
     **/
    public void afterupdate(){

        String YES="是";
        String ghzt_lost="已失效";
        String follow_status_dfp="待分配";
        //管户状态
        String ghzt=record_new.get("ghzt")==null?"":record_new.get("ghzt")+"";
        //是否分享
        String is_shared=record_new.get("is_shared")==null?"":record_new.get("is_shared")+"";
        //客户id
        String fk_account=record_new.get("fk_account")==null?"":record_new.get("fk_account")+"";
        //管户经理
        String fk_ccuser=record_new.get("fk_ccuser")==null?"":record_new.get("fk_ccuser")+"";

        if(ghzt.equals(ghzt_lost)){
            String sql_manager="select * from manage_group " +
                    "where ghzt!='"+ghzt_lost+"' " +
                    "and is_deleted='0' " +
                    "and fk_account='"+fk_account+"'";
            if(is_shared.equals(YES)){
                this.deleteShareObjectBySql("Account",
                        "parentid='"+fk_account+"' " +
                                "and userorgroupid='"+fk_ccuser+"' " +
                                "and accesslevel!='ALL'");
                //查询该客户是否还有其他管护权,如果没有更新客户状态，使之回退公海池
                List<CCObject> manage_group_other = this.cqlQuery("manage_group", sql_manager);
                if(manage_group_other.size()==0){
                    CCObject account = new CCObject("Account");
                    account.put("id",fk_account);
                    account.put("follow_status",follow_status_dfp);
                    this.update(account);
                }
            }else{
                String sql="select pool from ccuser where id='"+fk_ccuser+"'";
                List<CCObject> pool_list = this.cqlQuery("ccuser", sql);
                if(pool_list.size()>0){
                    String pool = pool_list.get(0).get("pool")==null?"": pool_list.get(0).get("pool")+ "";
                    CCObject account = new CCObject("Account");
                    account.put("id",fk_account);
                    account.put("ownerid",pool);
                    //查询该客户是否还有其他管护权,如果没有更新客户状态，使之回退公海池
                    List<CCObject> manage_group_other = this.cqlQuery("manage_group", sql_manager);
                    if(manage_group_other.size()==0){
                        account.put("follow_status",follow_status_dfp);
                    }
                    this.update(account);
                }
            }

        }

    }


}
